/*

*/
#ifndef SSD1306_H
#define SSD1306_H

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "component_ultrasonic.h"
#include "component_ir_avoidance.h"
#include "component_dht11.h"

#define SCREEN_WIDTH 128  // OLED display width,  in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);  // create SSD1306 display object connected to I2C

void oledDisplayCenter(String text) {
  int16_t x1;
  int16_t y1;
  uint16_t width = 0;
  uint16_t height = 0;
  oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
  // display on horizontal and vertical center
  oled.clearDisplay();  // clear display
  oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  oled.println(text);  // text to display
  oled.display();
}


void setup_ssd1306() {
  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true)
      ;
  }

  delay(2000);          // wait for initializing
  oled.clearDisplay();  // clear display
  oled.setTextSize(3);       // text size
  oled.setTextColor(WHITE);  // text color
  oled.setCursor(0, 10);     // position to display
  oledDisplayCenter("Tank");
  delay(1000);    
  oled.setTextSize(2); 
}

void loop_ssd1306() {
    String result_dht11_t,result_dht11_h;
    result_dht11_t = "T=" + String(temperature, 1) + "C";
    result_dht11_h = "H=" + String(humidity, 1) + "%";
    oled.clearDisplay(); 
    oled.setCursor(15,15);
    oled.print(result_dht11_t);
    oled.setCursor(15,40);
    oled.print(result_dht11_h);
    oled.display();
}


#endif /* SSD1306_H */